package hl.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import hl.dao.BaseDao;
import hl.model.TMember;
import hl.model.TMemberPhysical;
import hl.pageModel.DataGrid;
import hl.pageModel.MemberPhysical;
import hl.service.MemberPhysicalService;

@Service("memberPhysicalService")
public class MemberPhysicalServiceImpl implements MemberPhysicalService {

	private BaseDao<TMemberPhysical> memberPhysicalDao;

	public BaseDao<TMemberPhysical> getMemberPhysicalDao() {
		return memberPhysicalDao;
	}
	
	private BaseDao<TMember> memberDao;

	public BaseDao<TMember> getMemberDao() {
		return memberDao;
	}
	
	@Autowired
	public void setMemberDao(BaseDao<TMember> memberDao) {
		this.memberDao = memberDao;
	}
	
	@Autowired
	public void setMemberPhysicalDao(BaseDao<TMemberPhysical> memberPhysicalDao) {
		this.memberPhysicalDao = memberPhysicalDao;
	}

	@Override
	public DataGrid getDatagrid(MemberPhysical memberPhysical) {
		DataGrid dg = new DataGrid();
		String hql = "from TMemberPhysical t ";

		Map<String, Object> params = new HashMap<String, Object>();

		hql = search(memberPhysical, hql, params);

		if (memberPhysical.getOrder() != null) {
			hql += " order by " + memberPhysical.getSort() + " " + memberPhysical.getOrder();
		}

		List<TMemberPhysical> l = memberPhysicalDao.find(hql, params, memberPhysical.getPage(), memberPhysical.getRows());

		int totalHql = memberPhysicalDao.count(hql, params);

		List<MemberPhysical> pl = new ArrayList();
		if (l != null && l.size() > 0) {
			for (TMemberPhysical t : l) {
				MemberPhysical u = new MemberPhysical();
				BeanUtils.copyProperties(t, u);
				u.setPkMember(t.getTMember().getPkMember());
				u.setMemberName(t.getTMember().getName());
				pl.add(u);
			}
		}
		dg.setRows(pl);
		dg.setTotal(totalHql);
		return dg;
	}

	private String search(MemberPhysical memberPhysical, String hql, Map<String, Object> params) {
		if (memberPhysical.getMemberName() != null && !memberPhysical.getMemberName().trim().equals("")) {
			hql += " where t.TMember.name like :value1 ";
			params.put("value1", "%" + memberPhysical.getMemberName().trim() + "%");
		}
/*
		if (memberPhysical.getGender() != null && !memberPhysical.getGender().equals("")) {
			if (hql.contains("where"))
				hql += " and ";
			else
				hql += " where ";
			hql += " t.gender like :value2 ";
			params.put("value2", "%" + MemberPhysical.getGender().trim() + "%");
		}
		if (memberPhysical.getPkMemberPhysical() != null && !memberPhysical.getPkMemberPhysical().equals("")) {
			if (hql.contains("where"))
				hql += " and ";
			else
				hql += " where ";
			hql += " t.pkMemberPhysical like :value3 ";
			params.put("value3", "%" + memberPhysical.getPkMemberPhysical().toString() + "%");
		}*/
		return hql;
	}

	@Override
	public void save(MemberPhysical memberPhysical) {
		TMemberPhysical t = new TMemberPhysical();
		BeanUtils.copyProperties(memberPhysical, t);
		t.setGenerateDateime(new Date());
		double bmi =t.getWeight()/(Math.pow(t.getHeight()/100, 2));
		t.setBmi(String.valueOf(bmi));
		t.setTMember(memberDao.get(TMember.class, memberPhysical.getPkMember()));
		memberPhysicalDao.save(t);
	}

}
